<?php
class Sendmail extends CI_Controller {
	function __construct() {
		parent::__construct ();
		define ( "CACHE_PATH", "./application/cache" ); // 设置缓存文件放的地址
		$this->load->helper ( 'url' );
		$this->load->library ( 'commonclass' );
		$this->load->helper ( 'cache' );
		$this->load->library ( 'cache' );
		date_default_timezone_set ( 'PRC' ); // 其中PRC为“中华人民共和国”
		session_start();
	}
	function mail() {
		if (! isset ( $_POST ["code"] )) {
			$_to = $_POST [$_POST ["index"]];
			// 生成一个连接
			$one_str = to_guid_string ( $this->commonclass->rand_string () ); // 随机获得一个8位的字符串
			$this->S ( $one_str, $_to, 24 * 3600 );
		} else {
			$one_str = $_POST ["code"];
			$_to = $this->S ( $_POST ["code"] );
		}
		$config = Array (
				'protocol' => 'smtp',
				'smtp_host' => 'smtp.163.com', // 服务器
				'smtp_port' => 25,
				'smtp_user' => "geili0574",
				'smtp_pass' => '0m6YDR0y', // password
				'mailtype' => 'html', // 邮件类型
				'charset' => 'utf-8'  // 编码
	);
		$this->load->library ( 'email', $config );
		$this->email->set_newline ( "\r\n" );
		
		$from_name = "给力网"; // 发件人名称
		$email_subject = "找回密码"; // 标题
		
		$url = site_url ( "public/getpass/getpassresetpwd" ) . "?vstr=" . $one_str;
		$email_body = "
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<tbody>
	<tr>
		<td>
		<table cellpadding=0 cellspacing=0 width=100%>
        <tbody>
		<tr>
			<td width=25px; style=width:25px;></td>
			<td align=>
				<div style=line-height:40px;height:40px;></div>
				<p style=margin:0px;padding:0px;><strong style=font-size:14px;line-height:24px;color:#333333;font-family:arial,sans-serif;>亲爱的用户：</strong></p>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>您好！</p>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>您在" . date ( "Y年m月d日 H:i:s", time () ) . "提交了邮箱找回密码请求，请点击下面的链接修改密码。</p>
				<p style=margin:0px;padding:0px;><a href=" . $url . " style=line-height:24px;font-size:12px;font-family:arial,sans-serif;color:#0000cc>" . $url . "</a></p>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#979797;font-family:arial,sans-serif;>(如果您无法点击此链接，请将它复制到浏览器地址栏后访问)</p>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>为了保证您帐号的安全，该链接有效期为24小时，并且点击一次后失效！</p>
				<div style=line-height:80px;height:80px;></div>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>TB帐号中心</p>
				<p style=margin:0px;padding:0px;line-height:24px;font-size:12px;color:#333333;font-family:'宋体',arial,sans-serif;>" . date ( "Y年m月d日", time () ) . "</p>
			</td>
		</tr>
		</tbody>
		</table>
		</td>
	</tr>
</tbody>
</table>
</body>
</html>";
		
		// 解决手机提醒乱码问题
		$this->email->from ( "geili0574@163.com", $from_name ); // 发件人
		$this->email->to ( $_to ); // 收件人
		
		$this->email->subject ( $email_subject );
		$this->email->message ( $email_body );
		
		if (! $this->email->send ()) {
			if (isset ( $_POST ["code"] )) {
				return;
			}
			show_error ( $this->email->print_debugger () );
			return false;
		} else {
			if (isset ( $_POST ["code"] )) {
				echo '110000';
				return;
			}
			$in = strpos ( $_to, "@" );
			$str = $_to [0] . "*****" . substr ( $_to, $in - 1 );
			$lang ['error'] = '';
			$lang ["tomail"] = $str;
			$lang ["sta_getp"] = "getpasschoice";
			$lang ["code"] = $one_str;
			$this->load->view ( 'public/v_getpass_index', $lang );
		}
	}
	/**
	 * 缓存
	 */
	// 全局缓存设置和读取
	function S($name, $value = '', $expire = null, $type = '', $options = null) {
		static $_cache = array ();
		// 取得缓存对象实例
		$cache = new $this->cache ();
		if ('' !== $value) {
			if (is_null ( $value )) {
				// 删除缓存
				$result = $cache->rm ( $name );
				if ($result)
					unset ( $_cache [$type . '_' . $name] );
				return $result;
			} else {
				// 缓存数据
				$cache->set ( $name, $value, $expire );
				$_cache [$type . '_' . $name] = $value;
			}
			return;
		}
		if (isset ( $_cache [$type . '_' . $name] ))
			return $_cache [$type . '_' . $name];
			// 获取缓存数据
		$value = $cache->get ( $name );
		$_cache [$type . '_' . $name] = $value;
		return $value;
	}
}